<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller{

public function index(){
	$table = M('tbglobalvalue');
	$where['cname'] = 'client_package';
	$download = $table->field('cvalue')->where($where)->find();
	$this->assign('download',$download['cvalue']);
	$this->display();
}

public function worker(){

	$this->display();
}

	//排行榜
	public function rank(){

	$thisWeek= date('Y-m-d',strtotime("-1 Sunday"));   //本周日期
	$lastWeek = date('Y-m-d',strtotime("-2 Sunday")); //上周日期
	
	$thisMonth = date('Y-m-01');  //本月日期
	$lastMonth = date('Y-m-d',mktime(0,0,0,date('m')-1,1,date('Y'))); //上月日期

	$category = $_GET['cate'];
	if($category=='' || $category=='renqi'){   //财富排行和人气排行.财富排行默认为空
		if($category==''){
			$weekTable = 'tbuserconsumeweek';
			$monthTable = 'tbuserconsumemonth';
			$week_table = M("$weekTable");				
			$month_table = M("$monthTable");
		}else if($category=='renqi'){
			$weekTable = 'tbuserreceiveweek';
			$monthTable = 'tbuserreceivemonth';		
			$week_table = M("$weekTable");	
			$month_table = M("$monthTable");		
		}				
		$Tweek_data = $week_table->table("$weekTable a,tbuserinformation b")
				->field('a.nmoney,a.nuserid,b.calias')
				->where("a.nuserid=b.nuserid and a.ddate='$thisWeek'")
				->order('nmoney desc')
				->limit(30)
				->select();	
		$Lweek_data = $week_table->table("$weekTable a,tbuserinformation b")
				->field('a.nmoney,a.nuserid,b.calias')
				->where("a.nuserid=b.nuserid and a.ddate='$lastWeek'")
				->order('nmoney desc')
				->limit(30)
				->select();	
		$Tmonth_data = $month_table->table("$monthTable a,tbuserinformation b")
				->field('a.nmoney,a.nuserid,b.calias')
				->where("a.nuserid=b.nuserid and a.ddate='$thisMonth'")
				->order('nmoney desc')
				->limit(30)
				->select();	
		$Lmonth_data = $month_table->table("$monthTable a,tbuserinformation b")
				->field('a.nmoney,a.nuserid,b.calias')
				->where("a.nuserid=b.nuserid and a.ddate='$lastMonth'")
				->order('nmoney desc')
				->limit(30)
				->select();		
	}else if($category=='fangjian' || $category=='shuahua'){   //房间排列和房间刷花排列
			$weekTable = 'tbvcbscoreweek';
			$monthTable = 'tbvcbscoremonth';
			$week_table = M("$weekTable");				
			$month_table = M("$monthTable");

		if($category=='fangjian' ){
			$filed = 'fscore';
		}else if($category=='shuahua'){
			$filed = 'nmoney';
		}
		$Tweek_data = $week_table->table("$weekTable a,tbvcbinformation b")
				->field("a.$filed,a.nvcbid,b.cname")
				->where("a.nvcbid=b.nvcbid and a.ddate='$thisWeek'")
				->order("$filed desc")
				->limit(30)
				->select();	
		$Lweek_data = $week_table->table("$weekTable a,tbvcbinformation b")
				->field("a.$filed,a.nvcbid,b.cname")
				->where("a.nvcbid=b.nvcbid and a.ddate='$lastWeek'")
				->order("$filed desc")
				->limit(30)
				->select();	
		$Tmonth_data = $month_table->table("$monthTable a,tbvcbinformation b")
				->field("a.$filed,a.nvcbid,b.cname")
				->where("a.nvcbid=b.nvcbid and a.ddate='$thisMonth'")
				->order("$filed desc")
				->limit(30)
				->select();	
		$Lmonth_data = $month_table->table("$monthTable a,tbvcbinformation b")
				->field("a.$filed,a.nvcbid,b.cname")
				->where("a.nvcbid=b.nvcbid and a.ddate='$lastMonth'")
				->order("$filed desc")
				->limit(30)
				->select();		
	}
	
		$this->assign('thisweek',$Tweek_data);   //本周排名 
		$this->assign('lastweek',$Lweek_data);   //上周排名 
		$this->assign('thismonth',$Tmonth_data);   //本月排名 
		$this->assign('lastmonth',$Lmonth_data);   //上月排名 

		$this->display();
	}
    
	public function QQ(){
	if($_POST){
			$openid  = I('post.openId');
			$nickname= I('post.nickname');
			
			$G = M('tbuserinformation');
			$j = $G->where("cqqopenid='$openid'")->find();
			
	if(!$j){
		
			 //获取密码
			$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_ []{}<>~`+=,.;:/?|';
			$password = '';
		for ( $i = 0; $i < 10; $i++ ) {
			
			// 第一种是使用 substr 截取$chars中的任意一位字符；
			// 第二种是取字符数组 $chars 的任意元素
			// $password .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
		$password .= $chars[ mt_rand(0, strlen($chars) - 1) ];
		}
		
			$initpwd    = md5($password."a37b621cb27c2557ee52a681");
			$tbriagePsw = md5($briagePsw."a37b621cb27c2557ee52a681");
			$cloginpwd  = md5($tbriagePsw."bc860d113ea4310a9b268e3");
	     
				//获取随机产生的账号
			$total=M('tbuseridpool')->field('nuserid')->select();
		
			if($total){
			
			foreach($total as $val){
				$userIds[] = $val['nuserid'];	
			}
			 
		   if($userIds){
				$key = rand(0,count($userIds)-1);
				$id = $userIds[$key];
			
                $di['nuserid'] =$id;
                $di['nstartid'] =$id;
                $di['ncount']   =1;
                $ii['nuserid']  =$id;
				$ii['cloginpwd']=$cloginpwd;
				$ii['calias']   =$nickname;
				$ii['cqqopenid']=$openid;
				$ci['nuserid']=$id;
			    $Di = M('tbuseridalloc')->add($di);
				$Ii = M('tbuserinformation')->add($ii);
				$Ci = M('tbuseraccount')->add($ci); 
                $D =M(tbuseridpool)->where("nuserid='$id'")->delete();				
			    $flag = true;
				if(!$Di){
					$flag = false;				
				}
				if(!$Ii){
					$flag = false;				
				}
				if(!$Ci){
				$flag = false;		
				}				
				if($flag){				
				
				$data['tbriagePsw']=$tbriagePsw;//中间密码
			    $data['nuserid']=$id['nuserid'];//用户账号
			    $data['cqqopenid']=$openid;//qq的openid
				echo "登陆成功";
			//$direct = "qqlogin://".$openid.":".$id['nuserid'].":".$tbriagePsw; 
			//session('direct',$direct);
                $this->ajaxReturn($data,'JSON');
				}else{
				  echo "登录失败";
				} 
            } 
			}else{
            echo"账号发放完毕";			
			}	
		 }
		 else{
		
		$id =$G ->field('nuserid')
			    ->where("cqqopenid='$openid'")
		        ->find();
		//获取密码
			$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_ []{}<>~`+=,.;:/?|';
			$password = '';
		for ( $i = 0; $i < 10; $i++ ) {	
			// 第一种是使用 substr 截取$chars中的任意一位字符；
			// 第二种是取字符数组 $chars 的任意元素
			// $password .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
		$password .= $chars[ mt_rand(0, strlen($chars) - 1) ];
		}
		    $initpwd    = md5($password."a37b621cb27c2557ee52a681");
			$tbriagePsw = md5($briagePsw."a37b621cb27c2557ee52a681");
			$cloginpwd  = md5($tbriagePsw."bc860d113ea4310a9b268e3");		
	        $ii['cloginpwd']=$cloginpwd;
	        $Ii = M('tbuserinformation')->where("nuserid='$id'")
			                            ->save($ii);
			
			$data['tbriagePsw']=$tbriagePsw;//中间密码
			$data['nuserid']=$id['nuserid'];//用户账号
			$data['cqqopenid']=$openid;//qq的openid
			//$direct = "qqlogin://".$openid.":".$id['nuserid'].":".$tbriagePsw; 
			//session('direct',$direct);
            $this->ajaxReturn($data,'JSON');			
		} 
} 

}}
?>